/**
 * 
 */
package com.armail.client.controller.impl;

import java.util.Date;

import com.armail.client.controller.ComposeMailController;
import com.armail.client.controller.ComposeMailControllerListener;
import com.armail.client.controller.Controller;
import com.armail.client.controller.DraftsController;
import com.armail.client.controller.DraftsControllerListener;
import com.armail.client.controller.InboxController;
import com.armail.client.controller.NavigationController;
import com.armail.client.controller.SentItemsController;
import com.armail.client.di.Context;
import com.armail.client.dto.Mail;
import com.armail.client.view.NavigationView;
import com.armail.client.view.NavigationViewListener;
import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * @author arpit
 * 
 */
public class NavigationControllerImpl extends ControllerImpl implements
		NavigationController, NavigationViewListener,
		ComposeMailControllerListener, DraftsControllerListener {

	private final NavigationView navigationView;
	private final InboxController inboxController;
	private final ComposeMailController composeMailController;
	private final SentItemsController sentItemsController;
	private final DraftsController draftsController;
	private Controller activeController;

	@Inject
	public NavigationControllerImpl(Injector injector,
			NavigationView navigationView, InboxController inboxController,
			ComposeMailController composeMailController, SentItemsController sentItemsController, DraftsController draftsController) {
		super(injector);
		this.navigationView = navigationView;
		navigationView.addListener(this);

		this.inboxController = inboxController;
		this.composeMailController = composeMailController;
		this.composeMailController.addListener(this);
		
		this.sentItemsController = sentItemsController;
		this.draftsController = draftsController;
		this.draftsController.addListener(this);
	}

	@Override
	public void activate() {
		navigationView.attach();
		activate(inboxController);
	}

	@Override
	public void deactivate() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClickCompose() {
		composeMailController.setModel(createEmptyMail());
		activate(composeMailController);
	}
	
	private Mail createEmptyMail() {
		final Mail mail = new Mail();
		mail.setFromUser(Context.getUser());
		mail.setHasAttachment(false);
		mail.setRecievedDate(new Date());
		mail.setIsDraft(false);
		return mail;
	}

	@Override
	public void onMailSent() {
		activate(inboxController);
	}
	
	private void activate(Controller controller) {
		if(activeController != null) {
			activeController.deactivate();
		}
		activeController = controller;
		activeController.activate();
	}

	@Override
	public void onClickInbox() {
		activate(inboxController);
	}

	@Override
	public void onClickSentItems() {
		activate(sentItemsController);
	}

	@Override
	public void onClickDrafts() {
		activate(draftsController);
	}

	@Override
	public void onMailSaved() {
		activate(draftsController);
	}

	@Override
	public void onDraftSelected(Mail selectedMail) {
		composeMailController.setModel(selectedMail);
		activate(composeMailController);
	}

}
